package org.shj.algorithm.dhsjjg.tree;

//二叉树
public class BiTree<T> {
	
	private BiNode<T> root;
	
	public BiNode<T> getRoot() {
		return root;
	}

	public void setRoot(BiNode<T> root) {
		this.root = root;
	}

	//前序遍历
	public void preOrderTree(){
		if(root == null){
			System.out.println("Tree is empty.");
			return;
		}
		preOrder(root);
		
	}
	
	//中序遍历
	public void midOrderTree(){
		if(root == null){
			System.out.println("Tree is empty.");
			return;
		}
		midOrder(root);
	}
	
	//后序遍历
	public void postOrderTree(){
		if(root == null){
			System.out.println("Tree is empty.");
			return;
		}
		postOrder(root);
	}
	
	private void preOrder(BiNode<T> node){
		if(node == null){
			return;
		}
		System.out.print(node.getData());
		preOrder(node.getLeftChild());
		preOrder(node.getRightChild());
	}
	
	private void midOrder(BiNode<T> node){
		if(node == null){
			return ;
		}
		
		midOrder(node.getLeftChild());
		System.out.print(node.getData());
		midOrder(node.getRightChild());
	}
	
	private void postOrder(BiNode<T> node){
		if(node == null){
			return ;
		}
		
		postOrder(node.getLeftChild());		
		postOrder(node.getRightChild());
		System.out.print(node.getData());
	}
	
	public static void main(String[] args){
		BiTree<String> tree = new BiTree<String>();
		
		BiNode<String> root = new BiNode<String>();
		root.setData("A");
		
		BiNode<String> b = new BiNode<String>();
		b.setData("B");		
		root.setLeftChild(b);
		
		BiNode<String> c = new BiNode<String>();
		c.setData("C");
		root.setRightChild(c);
		
		BiNode<String> d = new BiNode<String>();
		d.setData("D");
		b.setLeftChild(d);
		
		BiNode<String> e = new BiNode<String>();
		e.setData("E");
		b.setRightChild(e);
		
		BiNode<String> f = new BiNode<String>();
		f.setData("F");
		c.setLeftChild(f);
		
		BiNode<String> g = new BiNode<String>();
		g.setData("G");
		c.setRightChild(g);
		
		BiNode<String> h = new BiNode<String>();
		h.setData("H");
		d.setLeftChild(h);
		
		BiNode<String> i = new BiNode<String>();
		i.setData("I");
		f.setLeftChild(i);
		
		BiNode<String> j = new BiNode<String>();
		j.setData("J");
		g.setRightChild(j);
		
		BiNode<String> k = new BiNode<String>();
		k.setData("K");
		h.setRightChild(k);
		
		tree.setRoot(root);
		
		tree.preOrderTree();
		System.out.println();
		tree.midOrderTree();
		System.out.println();
		tree.postOrderTree();
	}

}
